package com.timo.mail.util;

import com.timo.mail.model.MailBoxService;
import org.springframework.mail.MailMessage;
import org.springframework.util.Assert;

import javax.mail.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;

/**
 * @Description: 类用途：
 * @Author: Timo
 * @Date: 2019/7/16 16:16
 */
public class ReceiveMailHandler {

    /**
     * javaMail 的的session
     */
    private Session session;
    /**
     * 邮箱服务
     */
    private Store store;

    /**
     * 收件箱，发件箱，草稿箱
     */
    private Folder folder;


    private MailBoxService mailBoxService;


    public ReceiveMailHandler(MailBoxService mailBoxService) throws MessagingException {
        this.mailBoxService = mailBoxService;
        init();
    }

    public void test() throws MessagingException {
        System.out.println(folder.getMessageCount());
    }

    private void init() throws MessagingException {
        this.session = getSession();
        this.store = getStore();
        this.folder = session.getFolder(getURLName());
    }




    /**
     * 获取session
     * @return
     */
    private Session getSession() {
       return Session.getInstance(getProperties());
    }

    private Store getStore() throws NoSuchProviderException {
        return session.getStore(getURLName());
    }

    private URLName getURLName() {
        Assert.notNull(mailBoxService, "mailBoxService is null");
        return new URLName(
                mailBoxService.getProtocol().toLowerCase(),
                mailBoxService.getHost(),
                mailBoxService.getPort(),
                FolderType.INBOX.name,
                mailBoxService.getAddress(),
                mailBoxService.getPassword()
                );
    }

    private  Properties getProperties() {
        Assert.notNull(mailBoxService,"mailBoxService is null");

        Properties properties = new Properties();
        String protocol = mailBoxService.getProtocol().toLowerCase();
        String time_out = "30000";
        properties.setProperty("mail.imap.host", mailBoxService.getHost());
        properties.setProperty("mail.imap.connectiontimeout", time_out);
        properties.setProperty("mail.imap.timeout", time_out);
        properties.setProperty("mail.imap.connectionpooltimeout", time_out);

        // 关闭本地缓存
        properties.setProperty("mail.imap.partialfetch",false+"");
        // ssl set
        properties.setProperty("mail.imap.socketFactory.port", Integer.toString(mailBoxService.getPort()));
        properties.setProperty("mail.imap.ssl.enable", "true");
        properties.setProperty("mail.imap.ssl.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        return properties;
    }


    public enum FolderType{
        INBOX("INBOX");
        public String name;

        FolderType(String name) {
            this.name = name;
        }


    }


    public static void main(String[] args) throws MessagingException {

//        MailBoxService mailBox = new MailBoxService();
//        mailBox.setAddress("timo@storlead.com");
//        mailBox.setPassword("XXXXX");
//        mailBox.setProtocol("imap");
//        mailBox.setHost("imap.exmail.qq.com");
//        mailBox.setPort(993);
//        mailBox.setSsl(true);
//
//        ReceiveMailHandler mail = new ReceiveMailHandler(mailBox);
//
//        mail.test();


        List<String> list = new ArrayList<>();
        list.add("0");
        list.add("1");
        list.add("2");

        list.add("3");
        list.add("4");
        list.add("5");

        list.add("6");
        list.add("7");
        list.add("8");

        list.add("9");
        list.add("10");
        list.add("11");
        list.add("12");
        list.add("13");
        list.add("14");
        list.add("15");
        list.add("16");
        list.add("17");
        list.add("18");
//        list.add("19");


        int allCount = list.size();
        final int size = 5;
        int averageCount = (int) Math.floor(allCount / size);

        // 创建线程池

        // 提交线程
        for (int i = 0; i < size; i++) {
            int index = i * averageCount;
            int toIndex = (i + 1) * averageCount;
            if ((size - 1) <= i) {
                toIndex = allCount;
            }

            System.out.println(list.subList(index,toIndex));
        }
    }
}
